window.onload = function (){
    function httpPost(URL, PARAMS) {
        var temp = document.createElement("form");
        temp.action = URL;
        temp.method = "post";
        temp.style.display = "none";

        for (var x in PARAMS) {
            var opt = document.createElement("textarea");
            opt.name = x;
            opt.value = PARAMS[x];
            temp.appendChild(opt);
        }
        document.body.appendChild(temp);
        temp.submit();
    }
    var token = document.getElementById("csrftoken").innerText;
    var shareBtns = document.getElementsByClassName("shareBtn");
    for(let i = 0; i < shareBtns.length; i++){
        shareBtns[i].onclick = function () {
            var targetUserName = prompt("Target User Name:");
            if(targetUserName == null) {
                return;
            }
            var params = {
                noteid : shareBtns[i].name,
                target : targetUserName,
                _token : token
            }
            httpPost("ShareItem",params);
        }
    }
    var delBtns = document.getElementsByClassName("delBtn");
    for(let i = 0; i < shareBtns.length; i++){
        delBtns[i].onclick = function () {
            var  params = {
                id : delBtns[i].name,
                _token : token
            }
            httpPost("DeleteItem",params);
        }
    }
    var acceptBtns = document.getElementsByClassName("accept");
    for(let i = 0; i < acceptBtns.length; i++){
        acceptBtns[i].onclick = function () {
            var arguments = acceptBtns[i].name.split(",");
            var params = {
                source : arguments[1],
                sender : arguments[0],
                type : "accept",
                _token : token
            }
            httpPost("AcceptItem",params);
        }
    }
    var declineBtns = document.getElementsByClassName("decline");
    for(let i = 0; i < declineBtns.length; i++){
        declineBtns[i].onclick = function () {
            var arguments = acceptBtns[i].name.split(",");
            var params = {
                source : arguments[1],
                sender : arguments[0],
                type : "decline",
                _token : token
            }
            httpPost("AcceptItem",params);
        }
    }

}